package com.mxx.mxx.controller;



import com.mxx.mxx.common.core.controller.BaseController;
import com.mxx.mxx.common.core.domain.AjaxResult;
import com.mxx.mxx.dto.AccountDTO;
import com.mxx.mxx.dto.AccountUpdateDTO;
import com.mxx.mxx.po.Account;
import com.mxx.mxx.service.IAccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dronff
 * @since 2024-04-19
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {
    @Resource
    private IAccountService accountService;
    @PostMapping("/token")
    public AjaxResult getToken(@RequestParam()String phoneNumber, @RequestParam("password")String password) {
        return accountService.getToken(phoneNumber, password);
    }

    @PostMapping("/personalAccount")
    public AjaxResult add(@RequestBody AccountDTO account) {
        return accountService.add(account);
    }

    @GetMapping("/all")
    public AjaxResult getAll(){
        return AjaxResult.success("获取成功", accountService.list());
    }

    @DeleteMapping("/{userId}")
    public AjaxResult deleteOne(@PathVariable Integer userId){
        if (accountService.removeById(userId)) {
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }

    @PutMapping("/{userId}")
    public AjaxResult updateUser(@PathVariable Integer userId, @RequestBody AccountUpdateDTO account){
        return accountService.updateUser(userId, account);
    }

    @GetMapping("/userInfo")
    public AjaxResult getUserInfo(){
        Object userInfo = getLoginUser().getUser().getUserInfo();
        if (userInfo instanceof Account) {
            Account u = (Account) userInfo;
            return AjaxResult.success("查询成功", accountService.getById(u.getUserId()));
        }
        return AjaxResult.error("管理员无法查询");
    }
}
